package mall.generator;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
public class FileUtils {

    /**
     * 文件写入
     */
    public static void writeStringToFile(String content, String filePath) throws IOException {
        Path path = Paths.get(filePath);
        Files.createDirectories(path.getParent());
        Files.write(path, content.getBytes(StandardCharsets.UTF_8));
    }



    public static String readStringFromResource(String resourcePath) throws IOException, URISyntaxException {
        URL url = FileUtils.class.getClassLoader().getResource(resourcePath);
        if (url == null) {
            throw new FileNotFoundException("资源文件未找到: " + resourcePath);
        }

        Path path = Paths.get(url.toURI());
        byte[] bytes = Files.readAllBytes(path);
        return new String(bytes, StandardCharsets.UTF_8);
    }

}
